package com.example.scoredemo.fragment

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.NavAction
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import com.example.scoredemo.R

class HomeFragment : Fragment() {

    companion object {
        fun newInstance() = HomeFragment()
    }

    private lateinit var viewModel: HomeViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.home_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
        view?.findViewById<Button>(R.id.button3)?.setOnClickListener { navito() }
    }

    fun navito() {
        //fragment 传递参数
        val bundle = Bundle()
        bundle.putString("shatangju", "砂糖橘")
        var navHostController =
            activity?.supportFragmentManager?.findFragmentById(R.id.ragmentContainerView) as NavHostFragment
        //可以是action 也可以是fragment
        navHostController.navController.navigate(R.id.action_homeFragment_to_detailFragment, bundle)
    }
}
